package com.software.levelupandroid.home.ui.taskByPriority.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.software.levelupandroid.R;
import com.software.levelupandroid.entity.Task;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class ListViewAdapter extends BaseAdapter {
    private Context context;
    private List<Task> taskList;
    private LayoutInflater inflater;

    public ListViewAdapter(Context context, List<Task> taskList) {
        this.context = context;
        this.taskList = taskList;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return taskList.size();
    }

    @Override
    public Object getItem(int position) {
        return taskList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_list, parent, false);
            holder = new ViewHolder();
            holder.tvTaskTitle = convertView.findViewById(R.id.tv_taskTitle);
            holder.tvTimeStart = convertView.findViewById(R.id.tv_timeStart);
            holder.tvTimeEnd = convertView.findViewById(R.id.tv_timeEnd);
//            holder.tvTaskType = convertView.findViewById(R.id.tv_taskType);
//            holder.tvPriority = convertView.findViewById(R.id.tv_priority);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //获取任务并设置视图内容
        Task task = taskList.get(position);
        holder.tvTaskTitle.setText(task.getTaskTitle());
        String startTime = task.getStartTime();
        String endTime = task.getEndTime();


        try {
            SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm", Locale.getDefault());
            SimpleDateFormat outputFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
            Date date;
            Date startDate = inputFormat.parse(startTime);
//            Date endDate = inputFormat.parse(endTime);

            if (startTime != null){
                String str_startTime = startTime+"";
                String res_startTime = str_startTime.substring(str_startTime.length() - 1 - 5, str_startTime.length());
                if (endTime != null){
                    String str_endTime = endTime+"";

                    String res_endTime = str_endTime.substring(str_endTime.length() - 1 - 5, str_endTime.length());
                    holder.tvTimeEnd.setText(res_endTime);
                    holder.tvTimeStart.setText(res_startTime);
                }
            }

        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        SimpleDateFormat format1 = new SimpleDateFormat("HH:mm");
//        String s = format1.format(format1);


        System.out.print(startTime);
       // holder.tvTimeEnd.setText(task.getEndTime());
//        holder.tvTaskType.setText(getTaskType(task.getTaskType()));
//        holder.tvPriority.setText(getPriority(task.getPriority()));
        return convertView;
    }


    private String getTaskType(int type) {
        switch (type) {
            case 0:
                return "未设置开始结束时间的任务";
            case 1:
                return "通知型任务";
            case 2:
                return "计时型任务";
            case 3:
                return "系统任务";
            default:
                return "未知任务类型";
        }
    }
    private String getPriority(int priority) {
        switch (priority) {
            case 0:
                return "不重要不紧急";
            case 1:
                return "重要不紧急";
            case 2:
                return "紧急不重要";
            case 3:
                return "重要且紧急";
            default:
                return "未知优先级";
        }
    }
    private static class ViewHolder {
        TextView tvTaskTitle;
        TextView tvTimeStart;
        TextView tvTimeEnd;
        TextView tvTaskType;
        TextView tvPriority;
    }
}